package com.sx.gongzhong.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.sx.gongzhong.BaseActivity;
import com.sx.gongzhong.R;
import com.sx.gongzhong.adapter.MyFragmentPagerAdapter;
import com.sx.gongzhong.fragment.ExposureAllFragment;
import com.sx.gongzhong.model.PublicBean;
import com.sx.gongzhong.pest.PublicPest;
import com.sx.gongzhong.view.IPublicView;
import com.sx.gongzhong.widget.LoadDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**我的爆料
 * Created by admin on 2018/1/3.
 */

public class MyExposureActivity extends BaseActivity implements IPublicView {

    @BindView(R.id.tl_TabLayout)
    TabLayout tl_TabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private List<Fragment> fragments;
    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_my_exposure);
        setTitleName("我的爆料");
        ButterKnife.bind(this);
        LoadDialog.show(MyExposureActivity.this);
        new PublicPest(this).getPublicList("brokeNewsType");
    }

    private void initView(List<PublicBean> beanList) {
        List<String> titles=new ArrayList<>();
        titles.add("全部");
        fragments=new ArrayList<>();
        Bundle bundle=new Bundle();
        bundle.putBoolean("isMyAdd",true);
        ExposureAllFragment allFragments = new ExposureAllFragment();
        allFragments.setArguments(bundle);
        fragments.add(allFragments);
        tl_TabLayout.addTab(tl_TabLayout.newTab().setText(titles.get(0)));
        tl_TabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        for (int i=0;i<beanList.size();i++){
            PublicBean bean = beanList.get(i);
            titles.add(bean.getName());
            Bundle bundle1 = new Bundle();
            bundle1.putSerializable("Type",bean);
            bundle1.putBoolean("isMyAdd",true);
            ExposureAllFragment allFragment = new ExposureAllFragment();
            allFragment.setArguments(bundle1);
            fragments.add(allFragment);
            tl_TabLayout.addTab(tl_TabLayout.newTab().setText(titles.get(i+1)));
        }
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragments, titles);
        viewPager.setAdapter(pagerAdapter);
        //tabLayout加载viewpager
        tl_TabLayout.setupWithViewPager(viewPager);
    }


    @Override
    public void showError(String message) {
        LoadDialog.dismiss(MyExposureActivity.this);
    }

    @Override
    public void getPublicList(List<PublicBean> beanList) {
        LoadDialog.dismiss(MyExposureActivity.this);
        if (beanList!=null){
            initView(beanList);
        }
    }

}
